﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.Repositories
{
    public class MovieHallRepository : RepositoryInterfaces.IMovieHallRepository
    {
        /// <summary>
        /// Get movie hall for screen cast
        /// </summary>
        /// <param name="screenCast">Screen cast to find movie hall for</param>
        /// <returns>Movie hall for screen cast</returns>
        /// <exception cref="ArgumentNullException">Screen cast is null</exception>
        /// <exception cref="InvalidOperationException">More than one movie hall exists with provided screen cast</exception>
        /// <exception cref="Exception">Other exceptions</exception>
        public MovieHall GetMovieHall(ScreenCast screenCast)
        {
            try
            {
                using (BalderEntities _db = new BalderEntities())
                {
                    return _db.MovieHalls.Where(m => m.Id == screenCast.MovieHallId).Single();
                }
            }
            catch (ArgumentNullException e)
            {
                throw new ArgumentNullException("Screen casts is null" + Environment.NewLine + e.Message);
            }
            catch (InvalidOperationException e)
            {
                throw new InvalidOperationException("More than one movie hall contains the provided screen casts" + Environment.NewLine + e.Message);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Get movie hall from id
        /// </summary>
        /// <param name="id">Id of movie hall</param>
        /// <returns>Movie hall with id</returns>
        /// <exception cref="ArgumentNullException">Id is null</exception>
        /// <exception cref="InvalidOperationException">More than one movie hall exists with provided id</exception>
        /// <exception cref="Exception">Other exceptions</exception>
        public MovieHall GetMovieHall(int id)
        {
            try
            {
                using (BalderEntities _db = new BalderEntities())
                {
                    return _db.MovieHalls.Where(m => m.Id == id).Single();
                }
            }
            catch (ArgumentNullException e)
            {
                throw new ArgumentNullException("Id is null" + Environment.NewLine + e.Message);
            }
            catch (InvalidOperationException e)
            {
                throw new InvalidOperationException("More than one movie hall contains the provided id" + Environment.NewLine + e.Message);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Get all movie halls in cinema
        /// </summary>
        /// <param name="cinema">Cinema to find movie halls in</param>
        /// <returns>Movie halls in cinema</returns>
        /// <exception cref="ArgumentNullException">No cinema set</exception>
        /// <exception cref="Exception">Other exceptions</exception>
        public IList<MovieHall> GetMovieHalls(Cinema cinema)
        {
            try
            {
                using (BalderEntities _db = new BalderEntities())
                {
                    return _db.MovieHalls.Where(mh => mh.CinemaId == cinema.Id).ToList();
                }
            }
            catch (ArgumentNullException)
            {
                throw new ArgumentNullException("Cinema not set");
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
